--#include "data\functions\Activity\OpenServer\OpenServerCommon.lua" once
--时装打折活动

--是否在活动时间内
function IsInOpenServerFashionDisDay()
    local day = System.getDaysSinceOpenServer()
    local startDay = OpenServerCfg.fashiondisConfig.openday or 1
    local endDay = OpenServerCfg.fashiondisConfig.endday or OpenServerCfg.OpenServerDay
    if day >= startDay and day <= endDay then
        return true
    end
    return false
end


-- 打包数据
function SendOpenServerFashionDisData(sysarg)
	if not IsInOpenServerFashionDisDay() then
		return
	end
	local config = OpenServerCfg.fashiondisConfig
	if not config then 
		return
	end
	
    local sVar = Actor.getStaticVar(sysarg)
	
	if not sVar.osfdBuyCount then
        sVar.osfdBuyCount = {}
    end
	
	
	local pack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sOpenServerFashionDisData)
    if not pack then return end

    
	local dailyItemList = config.itemList	

	local itemLen = #dailyItemList
    DataPack.writeByte(pack, itemLen)
    --print("itemLen ="..itemLen)
    for idx = 1, itemLen do
        DataPack.writeByte(pack, idx)
        --print("idx ="..idx)
        local item = dailyItemList[idx]
        local lastNum = item.buylimit - (sVar.osfdBuyCount[idx] or 0)
        DataPack.writeShort(pack, lastNum)
        --print("lastNum = "..lastNum)
    end
	
	DataPack.flush(pack)
end


-- 操作，返回操作结果
function BuyOpenServerFashionDis(sysarg, clientPkt)
    if not IsInOpenServerFashionDisDay() then
        return
    end
    local config = OpenServerCfg.fashiondisConfig
    if config == nil then -- 参数错误
        print("error, config  error")
        return
    end
	
	local Index = DataPack.readByte(clientPkt)
	--print("Index ="..Index)
	
	local buyCount = DataPack.readWord(clientPkt)
	--print("buyCount ="..buyCount)
	
    local dailyItemList = config.itemList
		
	if Index > #dailyItemList then
            Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServer019, ttFlyTip)
    else
        local sVar = Actor.getStaticVar(sysarg)
		if not sVar then
            sVar = {}
        end

        local item = dailyItemList[Index]
        if not item then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServer019, ttFlyTip)
            return
        end
		
		if not sVar.osfdBuyCount then
			sVar.osfdBuyCount = {}
		end
		
		if not sVar.osfdBuyCount[Index] then
            sVar.osfdBuyCount[Index] = 0
        end
		
		if item.buylimit <= sVar.osfdBuyCount[Index] then
			Actor.sendTipmsg(sysarg, Lang.ScriptTips.OpenServer020, ttFlyTip)
			return 
		end
		
		if buyCount > item.buylimit - sVar.osfdBuyCount[Index] then
			buyCount = item.buylimit - sVar.osfdBuyCount[Index]
		end
		
		local buyCostMoney = item.nowprice * buyCount
		if buyCostMoney > Actor.getMoneyCount(sysarg, mtYuanbao) then
			Actor.sendItemLack( sysarg, qatYuanbao, 0, buyCostMoney )
			return
		end
		
		 if not Actor.delMoney( sysarg,mtYuanbao,buyCostMoney,LogIds.OpenServerFashionDisLogId,Lang.LogTips.OpenServerFashionDis) then
			return
		end
		sVar.osfdBuyCount[Index] = sVar.osfdBuyCount[Index] + buyCount
		--print("sVar.osfdBuyCount[Index] ="..sVar.osfdBuyCount[Index])
		
		
		local myAwards = GetAwardsByRate( {item}, buyCount )
		if IsBagGridEnough(sysarg, myAwards) then
			GiveCommonAward(sysarg, myAwards, LogIds.OpenServerFashionDisLogId, Lang.LogTips.OpenServerFashionDis)
		else
			local actorId = Actor.getActorId(sysarg)
			local title = Lang.ScriptTips.OpenServer023
			local content = Lang.ScriptTips.OpenServer024
			local sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
			local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)
			Awards.GiveToMailById(actorId, myAwards, title, content, LogIds.OpenServerFashionDisLogId, Lang.LogTips.OpenServerFashionDis, vocation, sex)
		end
		
		--SendOpenServerFashionDisData(sysarg)

		local pack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sOpenServerFashionDisOpt)
    	if not pack then return end
        DataPack.writeByte(pack, Index)
        --print("Index ="..Index)
        local item = dailyItemList[Index]
        local lastNum = item.buylimit - (sVar.osfdBuyCount[Index] or 0)
        DataPack.writeShort(pack, lastNum)
       	--print("lastNum = "..lastNum)
		DataPack.flush(pack)

	end	
end




	
	
	
	
	




